package com.swf.seed.security.cert;


import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.swf.seed.security.Algorithm;

/**
 * ClassName: SecurityMacSigner <br/>
 * Function: HMAC是密钥相关的哈希运算消息认证码（Hash-based Message Authentication Code）,
 * HMAC运算利用哈希算法，以一个密钥和一个消息为输入，生成一个消息摘要作为输出。。
 * 支持：HMACMD5,HMACRIPEMD160,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2014-7-2 下午7:34:33 <br/>
 * 
 * @author shenwf10476
 * @version 
 * @since JDK 1.6
 */
public class SecurityMacSigner extends SecuritySigner{
	 public static String initMacKey(String algorithm) throws Exception {   
		        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);   
		   
		        SecretKey secretKey = keyGenerator.generateKey();   
		        return SecurityBase64Signer.encryptBASE64(secretKey.getEncoded());   
		    }   

	public static String  signData(byte[] data,String key,String algorithm) throws Exception{
		SecretKey secretKey = new SecretKeySpec(SecurityBase64Signer.decryptBASE64(key), algorithm);
		Mac mac = Mac.getInstance(secretKey.getAlgorithm()); 
		mac.init(secretKey);   
	    return byteArrayToHexString(mac.doFinal(data));
	}
	
}
